﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

using LowLevelGraphics;
using LowLevelGraphics.Filter;
using LowLevelGraphics.Extractors;

namespace ImageRecognition2.Action
{
    /// <summary>
    /// 
    /// </summary>
    public class ActionSimpleBlobExtractor : AbstractCommand
    {
        public override void Run(object sender, EventArgs e)
        {
            UnsafeBitmap bitmap = MainFormSingleton.ActivePictureBox.BackgroundImage;

            BlobExtractor be = new BlobExtractor(bitmap);
            Blob[] aBlob = be.Detect();

            Color[] aColor = new Color[] { Definitions.Black, Definitions.Gray, Definitions.Green, Definitions.Red, Definitions.Yellow, Definitions.Magenta, Definitions.Cyan, Definitions.Blue };
            int nIndex = 0;

            int nColorLength = aColor.Length;
            foreach (Blob blob in aBlob)
            {
                if (blob == null) continue;
                nIndex++;
                nIndex %= nColorLength;

                foreach (Point point in blob.Points)
                {
                    bitmap.SetPixel(point, aColor[nIndex]);
                }
            }
            bitmap.UnlockImage();

            MainFormSingleton.ActivePictureBox.Invalidate();
        }
    }
}
